package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.common.constant.OrderStatusConstants;
import com.xr.common.enums.RedisDelayQueueEnum;
import com.xr.miniprogram.domain.*;
import com.xr.miniprogram.domain.vo.MiniProgramInsertOrderVO;
import com.xr.miniprogram.mapper.*;
import com.xr.miniprogram.service.IMiniProgramOrderUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.miniprogram.tools.RedissonUtil.RedisDelayQueueUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author da
 * @since 2023-09-19
 */
@Slf4j
@Service
public class MiniProgramOrderUserServiceImpl extends ServiceImpl<MiniProgramOrderUserMapper, MiniProgramOrderUser> implements IMiniProgramOrderUserService {

    @Resource
    private MiniProgramOrderMapper orderMapper;
    @Resource
    private MiniProgramServiceLogMapper serviceLogMapper;
    @Resource
    private MiniProgramOrderUserMapper orderUserMapper;

    @Override
    public Object updateOrderUser(MiniProgramOrderUser miniProgramOrderUser) {
        return this.updateById(miniProgramOrderUser);
    }

    @Transactional
    @Override
    public void updateStatusForComment(MiniProgramComment miniProgramComment) {
        Long miniProgramCommentId = miniProgramComment.getMiniProgramCommentId();
        MiniProgramServiceLog miniProgramServiceLog = serviceLogMapper.selectOne(new QueryWrapper<MiniProgramServiceLog>().eq("mini_program_comment_id", miniProgramCommentId));
        Long serviceLogId = miniProgramServiceLog.getServiceLogId();
        MiniProgramOrderUser miniProgramOrderUser = this.getByServiceLogId(serviceLogId);
        miniProgramOrderUser.setOrderStatusId(OrderStatusConstants.COMPLETION_ORDER);

        //如果已完成的数量=总服务次数-1，则修改对应order状态为服务完成
        int orderUserCount = this.count(new QueryWrapper<MiniProgramOrderUser>().lambda()
                .eq(MiniProgramOrderUser::getOrderId, miniProgramOrderUser.getOrderId())
                .eq(MiniProgramOrderUser::getOrderStatusId, OrderStatusConstants.COMPLETION_ORDER));
        MiniProgramOrder order = orderMapper.selectById(miniProgramOrderUser.getOrderId());
        if(orderUserCount == order.getServiceNumCount() - 1){
            order.setOrderStatusId(OrderStatusConstants.COMPLETION_ORDER);
            orderMapper.updateById(order);
        }
        this.updateById(miniProgramOrderUser);
    }

    @Override
    public MiniProgramOrderUser getByServiceLogId(Long serviceLogId) {
        return this.getOne(new QueryWrapper<MiniProgramOrderUser>().eq("service_log_id", serviceLogId));
    }

    @Override
    public MiniProgramOrderUser getById(Long orderUserId) {
        return baseMapper.selectById(orderUserId);
    }
}
